﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.FireProjects.Model;
using Yipusoft.FireProjects.BLL;
using Yipusoft.Common.Model;
using Yipusoft.Common.BLL;
using Yipusoft.SixAcquaint.BLL;
using Yipusoft.SixAcquaint.Model;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;
using Yipusoft.Vehicle.Model;
using Yipusoft.Vehicle.BLL;
using System.Xml;
using System.Xml.Linq;

public partial class BasicBase_FireProject_FireProjectNew : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string departmentId = "";
            if (Request["ID"] != null)
            {
                FireProjectInfo info = FireProject.getFireproject(new Guid(Request.QueryString["ID"]));
                departmentId = info.DepartmentID;
            }
            else
            {
                departmentId = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
            }
            BindDepartment();
            BindVDepartmentList();

            if (Yipusoft.Account.BLL.User.HasPermission(Page.User.Identity.Name, "201301"))//总队级
            {
                DepartmentList.Enabled = true;
                UnitList.Enabled = true;
                BindDepartmentUnit();

                VDepartmentList.Enabled = true;
                VUnitList.Enabled = true;
                BindVUnitList();

            }
            else if (Yipusoft.Account.BLL.User.HasPermission(Page.User.Identity.Name, "201302"))//支队级
            {
                DepartmentList.SelectedValue = departmentId.Substring(0, 4) + "000";
                DepartmentList.Enabled = false;
                UnitList.Enabled = true;
                BindDepartmentUnit();

                VDepartmentList.SelectedValue = departmentId.Substring(0, 4) + "000";
                VDepartmentList.Enabled = false;
                VUnitList.Enabled = true;
                BindVUnitList();
            }
            else if (Yipusoft.Account.BLL.User.HasPermission(Page.User.Identity.Name, "201310"))
            {
                DepartmentList.Enabled = false;
                UnitList.Enabled = false;
                DepartmentList.SelectedValue = departmentId.Substring(0, 4) + "000";


                BindDepartmentUnit();
                UnitList.SelectedValue = departmentId;


                VDepartmentList.Enabled = false;
                VUnitList.Enabled = false;
                VDepartmentList.SelectedValue = departmentId.Substring(0, 4) + "000";
                BindVUnitList();
                if (departmentId.Substring(4, 1) == "2")
                {
                    string Dadui = GetZhongDuiByDaDui(departmentId);
                    VUnitList.SelectedValue = Dadui;
                    if (Dadui == "" || departmentId == "1308222")
                    {
                        VUnitList.Enabled = true;
                    }
                }
                else if (departmentId.Substring(4, 1) == "1")
                {
                    VUnitList.Enabled = true;
                }
                else
                {
                    VUnitList.SelectedValue = departmentId;
                }

            }
            else if (Yipusoft.Account.BLL.User.HasPermission(Page.User.Identity.Name, "201303"))//本单位级
            {
                DepartmentList.Enabled = false;
                UnitList.Enabled = false;
                DepartmentList.SelectedValue = departmentId.Substring(0, 4) + "000";

                BindDepartmentUnit();
                UnitList.SelectedValue = departmentId;

                VDepartmentList.Enabled = false;
                VUnitList.Enabled = false;
                VDepartmentList.SelectedValue = departmentId.Substring(0, 4) + "000";
                BindVUnitList();
                VUnitList.SelectedValue = departmentId;
            }
            //BindClassList();
            //BindTypeList();
            BindVehicleList();
            BindVehicleView();

            List<Guid> VehicleIds = new List<Guid>();
            if (ViewState["VehicleIds"] == null)
            {
                ViewState["VehicleIds"] = VehicleIds;
            }

            init();

            BindVehicleGV();


        }
    }

    void BindVehicleGV()
    {
        List<Guid> VehicleIds = ViewState["VehicleIds"] as List<Guid>;
        ListPageContral1.ItemCount = VehicleIds.Count;
        ListPageContral1.SetFirstPage();
        gv.DataSource = VehicleIds;
        gv.DataBind();

    }

    public void ListPageContral1_PageSizeChange(int pageSize, int PageIndex)
    {
        gv.PageIndex = PageIndex;
        List<Guid> VehicleIds = ViewState["VehicleIds"] as List<Guid>;
        gv.DataSource = VehicleIds;
        gv.DataBind();
    }
    void BindVDepartmentList()
    {
        VDepartmentList.DataSource = Yipusoft.Account.BLL.Department.GetList(" (SUBSTRING(DepartmentID, 5, 3) = '000') ");
        VDepartmentList.DataBind();
    }

    void BindVUnitList()
    {

        VUnitList.Items.Clear();
        string strWhere = "(departmentid like '" + VDepartmentList.SelectedValue.Substring(0, 4) + "%'  and  SUBSTRING(DepartmentID, 5, 1)<>'0') or departmentid='" + VDepartmentList.SelectedValue + "'";
        VUnitList.DataSource = Department.GetList(strWhere);
        VUnitList.DataBind();
    }

    //void BindClassList()
    //{
    //    ClassList.DataSource = CodeItem.GetList2(" GroupID='XFZBLB' and ParentID='10' and ItemID<>'1099' order by SeqID,ItemID ");
    //    ClassList.DataBind();
    //}

    //void BindTypeList()
    //{
    //    TypeList.Items.Clear();
    //    TypeList.DataSource = CodeItem.GetList2(" GroupID='XFZBLB' and  parentID='" + ClassList.SelectedValue + "' order by ItemID,SeqID ");
    //    TypeList.DataBind();
    //}

    public void BindVehicleList()
    {
        VehicleList.Items.Clear();
        // string vehicleType = TypeList.SelectedValue;
        string VehicleDepartmentId = VUnitList.SelectedValue;
        string where = " DepartmentID='" + VehicleDepartmentId + "' ";
        VehicleList.DataSource = Vehicle.GetList(where);
        VehicleList.DataBind();
    }
    protected void UCDepartment1_selectValueChanged(string Velue)
    {
        BindddlUnit(Velue);

    }
    public void BindDepartment()
    {
        DepartmentList.DataSource = Yipusoft.Account.BLL.Department.GetList(" (SUBSTRING(DepartmentID, 5, 3) = '000') ");
        DepartmentList.DataBind();
    }

    public void BindDepartmentUnit()
    {
        string Departmentid = DepartmentList.SelectedValue;
        string strWhere = "(departmentid like '" + Departmentid.Substring(0, 4) + "%'  and  SUBSTRING(DepartmentID, 5, 1)<>'0')  or fullName like '%指挥中心%'  and   (DepartmentID LIKE '" + Departmentid.Substring(0, 4) + "%')  or fullName like '%司令%' and   (DepartmentID LIKE '" + Departmentid.Substring(0, 4) + "%') or fullName like '%警务%' and   (DepartmentID LIKE '" + Departmentid.Substring(0, 4) + "%') or fullName like '%警勤中队%' and   (DepartmentID LIKE '" + Departmentid.Substring(0, 4) + "%') or fullName like '%战训%' and   (DepartmentID LIKE '" + Departmentid.Substring(0, 4) + "%') or departmentId='" + Departmentid + "'";

        UnitList.DataSource = Yipusoft.Account.BLL.Department.GetList(strWhere);
        UnitList.DataBind();
    }
    void init()
    {

        string strWhere = " ParentID='MHJYYALB0000' ";
        List<CodeItemInfo> list = CodeItem.GetList(strWhere);
        ddlTypeID.Items.Add("");
        for (int i = 0; i < list.Count; i++)
        {
            ddlTypeID.Items.Add(new ListItem(list[i].ItemName, list[i].ItemID));
        }
        if (Request.QueryString["ID"] == null)
        {

            //添加
            if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "201300") || Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "201307"))
            {

                lbkWordView.Visible = false;
                caption.Text = "添加灭火预案";
                BindddlUnit(UnitList.SelectedValue);
            }
            else
            {
                Response.Write("<script>alert('没有权限！');history.back(0);</script>");
                return;
            }

        }
        else
        {//修改
            if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "201300") || Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "201308"))
            {
                caption.Text = "修改灭火预案";
                FireProjectInfo info = FireProject.getFireproject(new Guid(Request.QueryString["ID"]));
                DepartmentList.SelectedValue = info.DepartmentID.Substring(0, 4) + "000";
                UnitList.SelectedValue = info.DepartmentID;
                BindDepartmentUnit();

                ddlTypeID.SelectedValue = info.TypeID;
                BindDDlClassid();

                ddlClassID.SelectedValue = info.ClassID;
                txtDescription.Text = info.Description;
                txtProjectName.Text = info.ProjectName;
                lbkWordView.NavigateUrl = "WordFile/" + info.WordName;
                BindddlUnit(UnitList.SelectedValue);
                ddlUnit.SelectedValue = info.UnitId.ToString();
                KeyUnitView();
                string VehicleWhere = " ProjectID='" + info.ProjectID + "' ";
                List<FireProjectVehicleInfo> Vehiclelist = FireProjectVehicles.GetList(VehicleWhere);

                List<Guid> VehicleIds = ViewState["VehicleIds"] as List<Guid>;

                foreach (FireProjectVehicleInfo vehicle in Vehiclelist)
                {
                    Guid id = vehicle.VehicleId;
                    if (!VehicleIds.Contains(id))
                    {
                        VehicleIds.Add(id);
                    }
                }
                ViewState["VehicleIds"] = VehicleIds;
            }
            else
            {
                Response.Write("<script>alert('没有权限！');history.back(0);</script>");
                return;
            }
        }

    }

    void BindddlUnit(string Value)
    {
        ddlUnit.Items.Clear();
        if (ddlTypeID.SelectedValue == "MHJYYALB0300")
        {
            UnitTr.Visible = true;
            string departmentId = Value;
            if (departmentId.Substring(4, 1) == "0")//支队
            {
                string Where = "DepartmentID like '" + departmentId.Substring(0, 4) + "%'";
                List<KeyUnitInfo> list = KeyUnit.GetKeyUnitList(Where, "UnitName", "Asc", 100000, 0);
                ddlUnit.DataSource = list;
                ddlUnit.DataBind();
            }
            else if (departmentId.Substring(4, 1) == "2")//大队
            {
                string ZhongDuiDepart = GetZhongDuiByDaDui(departmentId);
                if (ZhongDuiDepart == "")
                {
                    string Where = "DepartmentID like '" + departmentId.Substring(0, 4) + "3%'";
                    List<KeyUnitInfo> list = KeyUnit.GetKeyUnitList(Where, "UnitName", "Asc", 10000, 0);
                    ddlUnit.DataSource = list;
                    ddlUnit.DataBind();
                }
                else
                {
                    string Where = "DepartmentID = '" + ZhongDuiDepart + "'";
                    string MangerZhongDiu = GetManagerZhongDiu(departmentId);

                    if (MangerZhongDiu != "")
                    {
                        Where = "DepartmentID in (" + MangerZhongDiu + ")";
                    }
                    List<KeyUnitInfo> list = KeyUnit.GetKeyUnitList(Where, "UnitName", "Asc", 10000, 0);
                    ddlUnit.DataSource = list;
                    ddlUnit.DataBind();
                }

            }
            else if (departmentId.Substring(4, 1) == "3")//中队
            {
                string Where = "DepartmentID = '" + departmentId + "'";
                List<KeyUnitInfo> list = KeyUnit.GetKeyUnitList(Where, "UnitName", "Asc", 10000, 0);
                ddlUnit.DataSource = list;
                ddlUnit.DataBind();
            }
        }
        else
        {
            UnitTr.Visible = false;

        }
        KeyUnitView();
    }

    string GetZhongDuiByDaDui(string DepartmentId)
    {
        DepartmentInfo DaDui = Department.GetDepartment(DepartmentId);

        string where = " departmentId like '" + DepartmentId.Substring(0, 4) + "3%' ";
        List<DepartmentInfo> list = Department.GetList(where);
        DepartmentInfo ZhongDui = list.Where(o => o.ShortName.Contains(DaDui.ShortName.Substring(0, 2))).FirstOrDefault();
        if (ZhongDui != null)
        {
            return ZhongDui.DepartmentID;
        }
        else
        {
            return "";
        }
    }

    private void KeyUnitView()
    {
        if (ddlUnit.SelectedValue != "")
        {
            lkbViewUnit.Visible = true;
            lkbViewUnit.NavigateUrl = "../KeyUnit/KeyUnitView.aspx?ID=" + ddlUnit.SelectedValue;
        }
        else
        {
            lkbViewUnit.Visible = false;
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        string path = Server.MapPath(".\\WordFile\\");
        string word = "";

        FireProjectInfo info = new FireProjectInfo();
        if (Request.QueryString["ID"] == null)
        {
            info.ProjectID = Guid.NewGuid();
        }
        else
        {
            info = FireProject.getFireproject(new Guid(Request.QueryString["ID"]));
            word = info.WordName;
        }
        if (UnitTr.Visible)
        {
            info.UnitId = new Guid(ddlUnit.SelectedValue);
        }
        else
        {
            info.UnitId = Guid.Empty;
        }
        info.ClassID = ddlClassID.SelectedValue;
        info.DepartmentID = UnitList.SelectedValue;
        info.Description = txtDescription.Text;
        info.ProjectName = txtProjectName.Text;
        info.SetAID = User.Identity.Name;
        
        info.TypeID = ddlTypeID.SelectedValue;
        info.UpdateAID = User.Identity.Name;
        info.UpdateTime = DateTime.Now;
        if (word != "")
        {
            info.WordName = word;
        }
        if (Request.QueryString["ID"] == null)//添加
        {
info.SetTime = DateTime.Now;
            if (WordFile.HasFile)
            {
                string ex = WordFile.FileName.Substring(WordFile.FileName.LastIndexOf('.') + 1);
                if (ex.ToLower() != "doc" && ex.ToLower() != "ppt")
                {
                    Prompt.Text = "提示:上传必须为Word文档或PPT文档!" + ex;
                    return;
                }
                else
                {
                    if (WordFile.FileContent.Length / 1024 / 1024 > 5)
                    {

                        Prompt.Text = "提示:上传文档不可大于5M!";
                        return;
                    }
                    else
                    {
                        word = Guid.NewGuid() + "." + ex;

                        WordFile.SaveAs(path + word);
                        info.WordName = word;
                    }
                }
                string id = FireProject.Insert(info).ToString();
                //添加车辆
                FireProjectVehicles.DeleteByProjectID(new Guid(id));
                List<Guid> VehicleIds = ViewState["VehicleIds"] as List<Guid>;
                foreach (Guid VehicleId in VehicleIds)
                {
                    FireProjectVehicleInfo vehicle = new FireProjectVehicleInfo();
                    vehicle.ProjectID = new Guid(id);
                    vehicle.VehicleId = VehicleId;
                    FireProjectVehicles.Insert(vehicle);
                }
                ///添加完成.
                EventLogInfo log = new EventLogInfo();
                log.EventTime = DateTime.Now;
                log.EventType = 1;
                log.EventData = "添加一个灭火预案";
                log.ItemID = id.ToString();
                log.ItemName = "BasicBase";
                log.ItemFullUrl = "FireProject/FireProjectNew.aspx";
                log.CreatedBy = Context.User.Identity.Name;
                log.CreatedTime = DateTime.Now;
                log.UserIP = Request.UserHostAddress;
                EventLog.Insert(log);

                init();
                Prompt.Text = "提示:添加成功!";

            }
            else
            {
                Prompt.Text = "提示:必须上传Word文档或PPT文档!";
                return;
            }

        }
        else
        {//修改
            if (WordFile.HasFile)
            {
                string ex = WordFile.FileName.Substring(WordFile.FileName.LastIndexOf('.') + 1);
                if (ex.ToLower() != "doc" && ex.ToLower() != "ppt")
                {
                    Prompt.Text = "提示:上传必须为Word文档或PPT文档!";
                    return;
                }
                else
                {
                    if (WordFile.FileContent.Length / 1024 / 1024 > 5)
                    {
                        Prompt.Text = "提示:上传文档不可大于5M!";
                        return;

                    }
                    else
                    {
                        word = info.WordName;
                        int Index = word.LastIndexOf(".");
                        word = word.Substring(0, Index) + "." + ex;
                        info.WordName = word;
                        System.IO.FileInfo file = new System.IO.FileInfo(path + word);
                        if (file.Exists)
                        {
                            file.IsReadOnly = false;
                        }
                        WordFile.SaveAs(path + word);
                    }
                }
            }
            FireProject.Update(info);

            //添加车辆
            string id = Request.QueryString["ID"];
            FireProjectVehicles.DeleteByProjectID(new Guid(id));
            List<Guid> VehicleIds = ViewState["VehicleIds"] as List<Guid>;
            foreach (Guid VehicleId in VehicleIds)
            {
                FireProjectVehicleInfo vehicle = new FireProjectVehicleInfo();
                vehicle.ProjectID = new Guid(id);
                vehicle.VehicleId = VehicleId;
                FireProjectVehicles.Insert(vehicle);
            }
            //修改完成
            EventLogInfo log = new EventLogInfo();
            log.EventTime = DateTime.Now;
            log.EventType = 1;
            log.EventData = "修改一个灭火预案";
            log.ItemID = Request.QueryString["ID"];
            log.ItemName = "BasicBase";
            log.ItemFullUrl = "FireProject/FireProjectNew.aspx";
            log.CreatedBy = Context.User.Identity.Name;
            log.CreatedTime = DateTime.Now;
            log.UserIP = Request.UserHostAddress;
            EventLog.Insert(log);
            init();
            Prompt.Text = "提示:修改成功!";


        }


    }

    void BindDDlClassid()
    {
        if (ddlTypeID.SelectedValue != "")
        {
            ddlClassID.Items.Clear();
            ddlClassID.Items.Add("");
            List<CodeItemInfo> list = CodeItem.GetList(" ParentID='" + ddlTypeID.Text + "' ");

            for (int i = 0; i < list.Count; i++)
            {
                ddlClassID.Items.Add(new ListItem(list[i].ItemName, list[i].ItemID));
            }
        }
        else
        {
            ddlClassID.Items.Clear();
        }
    }



    protected void ddlTypeID_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDDlClassid();
        BindddlUnit(UnitList.SelectedValue);
    }
    protected void lbkWordView_Click(object sender, EventArgs e)
    {

    }
    protected void DepartmentList_TextChanged(object sender, EventArgs e)
    {
        //System.Threading.Thread.Sleep(6000);
        BindDepartmentUnit();
        BindddlUnit(UnitList.SelectedValue);
    }
    protected void UnitList_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindddlUnit(UnitList.SelectedValue);
    }
    protected void ddlUnit_SelectedIndexChanged(object sender, EventArgs e)
    {
        KeyUnitView();
    }
    protected void VDepartmentList_SelectedIndexChanged(object sender, EventArgs e)
    {
        // System.Threading.Thread.Sleep(6000);
        BindVUnitList();
        BindVehicleList();
        BindVehicleView();
    }
    protected void ClassList_SelectedIndexChanged(object sender, EventArgs e)
    {
        //BindTypeList();
        BindVehicleList();
        BindVehicleView();
    }
    protected void VehicleList_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindVehicleView();
    }

    private void BindVehicleView()
    {
        //Guid VehicleId = new Guid(VehicleList.SelectedValue);
        //VehicleInfo info = Vehicle.GetDetail(VehicleId);
        //if (info != null)
        //{
        if (VehicleList.Items.Count > 0)
        {
            hylviewVehicle.Visible = true;
            hylviewVehicle.NavigateUrl = "../Vehicle/VehicleView.aspx?id=" + VehicleList.SelectedValue;
        }
        else
        {
            hylviewVehicle.Visible = false;
        }

        //}
    }
    protected void TypeList_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindVehicleList();
        BindVehicleView();
    }
    protected void VUnitList_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindVehicleList();
        BindVehicleView();
    }
    //添加车辆
    protected void Button1_Click(object sender, EventArgs e)
    {
        List<Guid> VehicleIds = ViewState["VehicleIds"] as List<Guid>;
        Guid id = new Guid(VehicleList.SelectedValue);
        if (!VehicleIds.Contains(id))
        {
            VehicleIds.Add(id);
        }

        ViewState["VehicleIds"] = VehicleIds;
        BindVehicleGV();
    }
    protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = ((ListPageContral1.CurrentPageIndex * ListPageContral1.PageSize) + (e.Row.RowIndex + 1)).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");

            Guid id = new Guid(e.Row.DataItem.ToString());
            VehicleInfo info = Vehicle.GetDetail(id);
            if (info != null)
            {
                //车辆名称
                HyperLink VehicleName = e.Row.FindControl("VehicleName") as HyperLink;
                VehicleName.Text = info.VehicleName;
                VehicleName.NavigateUrl = "../Vehicle/VehicleView.aspx?id=" + id;

                Label lblclass = e.Row.FindControl("lblclass") as Label;
                lblclass.Text = GetType(info.VehicleClass);

                Label lblType = e.Row.FindControl("lblType") as Label;
                lblType.Text = GetCodeItem("XFZBLB", info.VehicleClass);

                Label lbldepartmentName = e.Row.FindControl("lbldepartmentName") as Label;
                lbldepartmentName.Text = GetDepartmentName(info.DepartmentID);
            }
            ImageButton imgbtnDelete = e.Row.FindControl("imgbtnDelete") as ImageButton;
            imgbtnDelete.CommandArgument = id.ToString();


        }
    }

    public string GetType(string itemID)
    {
        CodeItemInfo Item = CodeItem.GetCodeItem(itemID, "XFZBLB");
        CodeItemInfo Pitem = CodeItem.GetCodeItem(Item.ParentID, "XFZBLB");
        return Pitem.ItemName;
    }
    protected string GetDepartmentName(string data)
    {
        return Department.GetDepartment(data).FullName;
    }

    public string GetCodeItem(string GroupID, string deviceClass)
    {
        Yipusoft.Common.Model.CodeItemInfo info = CodeItem.GetCodeItem(deviceClass, GroupID);
        return info != null ? info.ItemName : "";
    }

    protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Del")
        {
            List<Guid> VehicleIds = ViewState["VehicleIds"] as List<Guid>;
            Guid id = new Guid(e.CommandArgument.ToString());
            if (VehicleIds.Contains(id))
            {
                VehicleIds.Remove(id);
            }
            BindVehicleGV();
        }
    }
    string GetManagerZhongDiu(string Departmentid)
    {
        if (Departmentid == "1308222")
        {
            return "'1308301','1308303','1308305','1308307','1308373'";
        }else
        {
            return "";
        }
    }
}
